/**
 * 
 */
package it.unitn.testbed.client.panels;

import gwtupload.client.IUploader;
import gwtupload.client.SingleUploader;
import gwtupload.client.IUploadStatus.Status;
import it.unitn.testbed.client.Testbed;
import it.unitn.testbed.client.common.MoteConfig;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * The setup panel where users can load XML descriptors of the testbed.
 * 
 * @author Stefan Guna
 * 
 */
public class SetupPanel extends VerticalPanel {

	private class ConfigCallback implements AsyncCallback<MoteConfig[]> {
		public void onFailure(Throwable caught) {
			testbed.setStatus("Error refreshing testbed: "
					+ caught.getMessage());
		}

		public void onSuccess(MoteConfig[] result) {
			int row = 1;
			for (MoteConfig mote : result) {
				motesTable.setText(row, 0, mote.getId().toString());
				motesTable.setText(row, 1, mote.getSource());
				motesTable.setText(row, 3, mote.getSerialFw());
				if (mote.getSshSFStart() != null)
					motesTable.setText(row, 4, mote.getSshSFStart());
				if (mote.getSshSFStop() != null)
					motesTable.setText(row, 5, mote.getSshSFStop());
				row++;
			}
		}
	}

	private class ConfigUploadFinish implements
			IUploader.OnFinishUploaderHandler {
		public void onFinish(IUploader uploader) {
			testbed.newTaskPanel(false);
			testbed.getTestbedService().getTestbedConfig(new ConfigCallback());

			if (uploader.getStatus() == Status.SUCCESS) {
				testbed.setStatus(uploader.getFileName()
						+ " uploaded successfully.");
			}
			if (uploader.getStatus() == Status.ERROR) {
				testbed.setStatus("Error in uploading "
						+ uploader.getFileName() + ": "
						+ uploader.getServerResponse());
			}
		}
	}

	private FlexTable motesTable;

	private Testbed testbed;

	public SetupPanel(Testbed testbed) {
		this.testbed = testbed;

		final VerticalPanel formHolder = new VerticalPanel();
		Label infoLabel = new Label("Select configuration descriptor:");
		Button send = new Button("Upload configuration");

		formHolder.add(infoLabel);

		FormPanel form = new FormPanel() {
			{
				super.add(formHolder);
			}

			public void add(Widget w) {
				formHolder.add(w);
			}
		};

		SingleUploader configUploader = new SingleUploader(null, send, form);
		formHolder.add(send);
		configUploader.setServletPath("send.config");
		String validExtensions[] = new String[1];

		validExtensions[0] = "xml";
		configUploader.setValidExtensions(validExtensions);
		configUploader.addOnFinishUploadHandler(new ConfigUploadFinish());

		motesTable = new FlexTable();
		motesTable.setText(0, 0, "ID");
		motesTable.setText(0, 1, "Connection");
		motesTable.setText(0, 3, "SF Location");
		motesTable.setText(0, 4, "SSH SF start");
		motesTable.setText(0, 5, "SSH SF stop");
		testbed.getTestbedService().getTestbedConfig(new ConfigCallback());

		add(form);
		add(new HTML("<a href=\"config-download\">download config</a>"));
		add(motesTable);
	}
}
